//
//  Spreadsheet.swift
//  FlyweightPattern
//
//  Created by LY on 2017/11/2.
//  Copyright © 2017年 DesignPatterns. All rights reserved.
//

import Foundation

class Coordinate: Hashable {
    let col : Character
    let row : Int
    init(col:Character,row:Int) {
        self.col = col
        self.row = row
    }
    var description:String{
        return "\(col) \(row)"
    }
    var hashValue: Int{
        return description.hashValue
    }
    static func == (lhs: Coordinate, rhs: Coordinate) -> Bool {
        return lhs.col == rhs.col && lhs.row == rhs.row
    }
}

class Cell {
    var coordinate : Coordinate
    var value : Int
    init(col:Character,row:Int,val:Int) {
        coordinate = Coordinate(col: col, row: row)
        value = val
    }
}

class Spreadsheet{
    var grid : FlyWeight// Dictionary<Coordinate,Cell>()
    init() {
//        let letters : String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
//        var stringIndex = letters.startIndex
//        let rows = 50
//        repeat{
//            let collectter = letters[stringIndex]
//            stringIndex = letters.index(after: stringIndex)
//            for rowIndex in 1 ... rows{
//                let cell = Cell(col: collectter, row: rowIndex, val: rowIndex)
//                grid[cell.coordinate] = cell
//            }
//        }while (stringIndex != letters.endIndex)
        grid = FlyweightFactory.createFlyWeight()
    }
    func setValue(coord:Coordinate,value:Int){
//        grid[coord]?.value = value
        grid.setSunScription(index: coord, value: value)
    }
    var total:Int{
//        return grid.values.reduce(0, { (total, cell) -> Int in
//            return total + cell.value
//        })
        return grid.total
    }
}

protocol FlyWeight {
    func getSubScription(index:Coordinate) -> Int?
    func setSunScription(index:Coordinate,value:Int?) 
    var total:Int{get}
    var count:Int{get}
}
